<?php

App::run();



class App {
    static $config = [] ;
    static $module = '';
    static $file = '';
    static $password = '' ;
    static $isLogin = false ;
    public  static function run(){
        session_start() ;
        $config = include './config.php' ;
        self::$password = $config['password'] ;
        self::$config = $config['dirs'];
        self::$module = isset($_GET['m']) ? $_GET['m'] : '' ;
        self::$file = isset($_GET['f']) ? $_GET['f'] : '' ;

        if(isset($_SESSION['_logsys_auth']) &&  $_SESSION['_logsys_auth']== md5($_SERVER['HTTP_HOST'].$_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'])){
            self::$isLogin = true ;
        }

        if(self::$module == 'auth'){
            return self::auth();
        }else{
            if(self::$isLogin == false && self::$module != ''){
                header("Location:?");
                exit ;
            }
        }


        if(self::$module != '' && self::$file == ''){
            return self::listFile();
        }

        if (self::$module != '' && self::$file != ''){
            self::$file = self::$config[self::$module]['logdir'].'/'.self::$file ;
            return self::showFile() ;
        }

        return self::listModule();
    }

    public static function listModule(){
        include __DIR__.'/tpl/list_model.tpl.php' ;
    }


    public static function listFile(){
        $dir = self::$config[self::$module]['logdir'] ;
        include __DIR__.'/tpl/list_file.tpl.php' ;
    }


    public static function showFile(){
        include __DIR__.'/tpl/show_file.tpl.php' ;
    }


    public static function auth(){
        $action = $_GET['a'] ?: '' ;
        if($action == 'login'){
            $password = $_POST['password'] ;
            if($password  == self::$password){
                $_SESSION['_logsys_auth'] = md5($_SERVER['HTTP_HOST'].$_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR']) ;
                echo  json_encode(['code' => 0]) ;
            }else{
                echo  json_encode(['code' => -1 ]) ;
            }
        }

        if($action  == 'logout'){
            unset($_SESSION['_logsys_auth']) ;
            header("Location:?");
            exit ;
        }
    }
}


function scandirFiles($dir){
    static  $rs = [] ;
    $files = scandir($dir) ;
    foreach ($files as $file){
        $trueFile = $dir.'/'.$file ;
        if($file != '.' && $file != '..'){
            if (is_dir($trueFile)){
                scandirFiles($trueFile) ;
            }else{
                $rs[] = $trueFile ;
            }
        }
    }
    return $rs ;
}










